/**
 * 自动加载路由配置
 */

var fs = require('fs');

var home = require('./routes/home/index.js'); //前台首页路由
var admin = require('./routes/admin/index.js'); //后台首页路由
/**
 * 所有经过admin的接口都需要校验是否登录
 * @param req
 * @param res
 * @param next
 */
function checkUserLogin(req,res,next){
  if(req.session.user ){
    next()
  }else{

    res.format({

      'application/json': function(){
        res.json({code: '-1', msg: '请重新登录', data: null});
      },

      'default': function() {
        res.redirect('/admin/login');
      }
    });

  }
}

module.exports = function(app){

  /**
   * 自动导入路由配置,默认匹配后台路由
   */
  fs.readdirSync(__dirname+'/routes/admin/').forEach(function(name){
    var name = name.replace(/.js/,'');
    var obj = require('./routes/admin/' + name);
    if(name !== 'index'){
      app.use('/admin/'+name , checkUserLogin, obj);
    }
  });

  app.use('/admin',admin); //后台路由
  app.use('/',home); //首页路由

};